package com.usian.reggie.controller;

import com.github.pagehelper.PageInfo;
import com.usian.reggie.common.R;
import com.usian.reggie.entity.Employee;
import com.usian.reggie.entity.dto.SetmealDto;
import com.usian.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {


    @Autowired
    private SetmealService setmealService;

    /**
     * 套餐保存
     *
     * @param setmealDto
     * @param session
     * @return
     */
    @PostMapping("/save")
    public R save(@RequestBody SetmealDto setmealDto, HttpSession session){
        Employee employee = (Employee) session.getAttribute("user");
        if(employee!=null){
            setmealDto.setCreateUser(employee.getId());
            setmealDto.setUpdateUser(employee.getId());
        }
        setmealService.save(setmealDto);
        return R.success("保存成功");
    }

    /**
     * 套餐分页查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R page(@RequestParam(defaultValue = "1")Integer page,
                  @RequestParam(defaultValue = "10")Integer pageSize,
                  String name){
        PageInfo<SetmealDto> pageInfo = setmealService.findByPage(page, pageSize, name);
        return R.success(pageInfo);
    }

    /**
     * 启售、批量启售、停售、批量停售
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public R status(@PathVariable Integer status,String ids,HttpSession session){
        Employee employee = (Employee) session.getAttribute("user");
        String[] strings = ids.split(",");
        for(String id:strings){
            SetmealDto setmealDto =new SetmealDto();
            setmealDto.setId(Long.parseLong(id));
            setmealDto.setStatus(status);
            if(employee!=null){
                setmealDto.setUpdateUser(employee.getId());
            }
            setmealService.update(setmealDto);
        }
        return R.success("修改成功");
    }
}
